﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Model;
using DataServices;

namespace LFWeb3.Controllers
{
    public class AdviserController : Controller
    {
        DaoAdviser AdviserDao = new DaoAdviser();

        public ActionResult Adviser()
        {
            IList<AdviserInfo> advisers = new List<AdviserInfo>();
            advisers = AdviserDao.LoadAllAdvisers();
            ViewBag.adviser = advisers;
            return View();
        }
        public ActionResult Add()
        { 
            return View();
        }

        public ActionResult Edit()
        {
            ViewBag.Adviser = AdviserDao.LoadAllAdvisers();
            return View();
        }

        public ActionResult Update(string id)
        {
            AdviserInfo adviser = AdviserDao.LoadAdviserByID(id);
            ViewBag.adviser = adviser;
            return View();
        }

        public ActionResult ViewAdviser(string id)
        {
            IList<AdviserInfo> advisers = new List<AdviserInfo>();
            advisers = AdviserDao.LoadAllAdvisers();
            ViewBag.advisers = advisers;
            ViewBag.adviser = AdviserDao.LoadAdviserByID(id);
            return View();
        }

        [ValidateInput(false)]
        public JsonResult AddAdviser(AdviserInfo adviser)
        {
            try
            {
                adviser.AdviserID = Guid.NewGuid().ToString();
                AdviserDao.AddAdviser(adviser);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public JsonResult DeleteAdviser(string id)
        {
            try
            {
                AdviserDao.DeleteAdviser(id);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        //UpdateAdviser
        [ValidateInput(false)]
        public JsonResult UpdateAdviser(AdviserInfo adviser)
        {
            try
            {
                AdviserDao.UpdateAdviser(adviser);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }
    }
}